﻿using System.Drawing;
using System.Windows.Forms;

namespace DCS.Extensions.WindowsForms
{
    public static class ControlExtensionMethods
    {
        public static void SafeVisibleSet(this Control control, bool value)
        {
            control.InvokeSync(() =>  control.Visible = value );
        }
        public static bool SafeVisibleGet(this Control control)
        {
            return control.InvokeSync(() => control.Visible);
        }

        public static void SafeTextSet(this Control control, string value)
        {
            control.InvokeSync(() => control.Text = value);
        }
        public static string SafeTextGet(this Control control)
        {
            return control.InvokeSync(() => control.Text);
        }

        public static void SafeValueSet(this ProgressBar control, int value)
        {
            control.InvokeSync(() => control.Value = value);
        }

        public static int SafeValueGet(this ProgressBar control)
        {
            return control.InvokeSync(() => control.Value);
        }

        public static void SafePictureBoxSet(this PictureBox control, Image value)
        {
            control.InvokeSync(() =>
                                   {
                                       if (control.Image != null)
                                       {
                                           control.Image.Dispose();
                                       }

                                       control.Image = value;
                                   });
        }

        public static Image SafePictureBoxGet(this PictureBox control)
        {
            return control.InvokeSync(() => control.Image);
        }

        public static void SafeEnable(this Control control)
        {
            control.InvokeSync(() => control.Enabled = true);
        }
        
        public static void SafeDisable(this Control control)
        {
            control.InvokeSync(() => control.Enabled = false);
        }

        public static void SafeFocus(this Control control)
        {
            control.InvokeSync(() => control.Focus());
        }

        public static void SafeDispose(this Control control)
        {
            control.InvokeSync(control.Dispose);
        }

        public static void SafeRowAdd(this DataGridView dataGrid, params object[] parameters)
        {
            dataGrid.InvokeSync(() => { dataGrid.Rows.Add(parameters); });
        }

        public static void SafeBackColorSet(this Control control, Color color)
        {
            control.InvokeSync(() => control.BackColor = color);
        }

        public static void SafeNodsClear(this TreeView treeView)
        {
            treeView.InvokeSync(() => treeView.Nodes.Clear());
        }

        public static void SelectedTabSafeSet(this TabControl tabControl, TabPage page)
        {
            tabControl.InvokeSync(() => tabControl.SelectedTab = page);
        }

        public static void SafeInvalidate(this Control control)
        {
            control.InvokeSync(control.Invalidate);
        }
    }
}